package controller;

import dao.UserDao;
import model.User;
import view.UI;

import java.sql.Connection;

public class LoginController {

    public LoginController() {
    }

    int role = 1;

    /**
     * 显示登录选项
     * 记录登录信息
     */
    public void login() {
        this.role = UI.role();
        switch (this.role) {
            case 1:
                this.userLogin();
                break;
            case 3:
                this.userRegister();
                break;

        }

    }

    public void userLogin() {
        User user = UI.login();
        user.setRole(this.role);
        UserDao userDao = new UserDao();
        user = userDao.login(user);
        if (user.getUserId() > 0) {
            int r = UI.book_list(1);
        } else {
            System.out.println("没有该用户信息");
        }


    }


    public void userRegister() {
        User user = UI.userRegister();
        if (user != null) {
            UserDao userDao = new UserDao();
            int res = userDao.addUser(user);
            if (res == 0) {
                System.out.println("注册成功请登录");
                this.userLogin();
            }
        }
    }

}
